package com.hospital.diagnosis.module.service.impl;

import com.hospital.common.dto.data.simulation.InspectionReportDto;
import com.hospital.common.dto.diagnosis.module.DiagnosisApplicationDTO;
import com.hospital.common.dto.diagnosis.module.DiagnosisRecordDTO;
import com.hospital.common.dto.payment.module.OrderCreateDto;
import com.hospital.common.enums.payment.module.OrderType;
import com.hospital.common.pojo.*;
import com.hospital.common.vo.data.simulation.InspectionReportVo;
import com.hospital.diagnosis.module.exception.ServiceException;
import com.hospital.diagnosis.module.feign.HospitalFeign;
import com.hospital.diagnosis.module.feign.PaymentFeign;
import com.hospital.diagnosis.module.mapper.DiagnosisRecordMapper;
import com.hospital.diagnosis.module.service.DiagnosisRecordService;
import com.hospital.diagnosis.module.utils.JsonUtils;
import com.hospital.diagnosis.module.vo.DiagnosisRecordTitleVo;
import com.hospital.diagnosis.module.vo.DiagnosisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.hospital.diagnosis.module.exception.enums.ExceptionCode.IMPORT_RECORD_FAILED;

@Service
public class DiagnosisRecordServiceImpl implements DiagnosisRecordService
{
    @Autowired
    private DiagnosisRecordMapper diagnosisRecordMapper;
    @Autowired
    private HospitalFeign hospitalFeign;
    @Autowired
    private PaymentFeign paymentFeign;

    @Override
    public List<DiagnosisRecordTitleVo> getDiagnosisRecordListByPatientId(Long patientId, Integer pageNum, Integer pageSize)
    {
        return diagnosisRecordMapper.getDiagnosisRecordListByPatientId(patientId, (pageNum - 1) * pageSize, pageSize);
    }

    @Override
    public Integer getDiagnosisRecordCountByPatientId(Long patientId)
    {
        return diagnosisRecordMapper.getDiagnosisRecordCountByPatientId(patientId);
    }

    @Override
    public List<DiagnosisRecordTitleVo> getDiagnosisRecordListByDoctorId(Long doctorId, Integer pageNum, Integer pageSize)
    {
        return diagnosisRecordMapper.getDiagnosisRecordListByDoctorId(doctorId, (pageNum - 1) * pageSize, pageSize);
    }

    @Override
    public Integer getDiagnosisRecordCountByDoctorId(Long doctorId)
    {
        return diagnosisRecordMapper.getDiagnosisRecordCountByDoctorId(doctorId);
    }

    @Override
    public TDiagnosisRecord getDiagnosisRecordById(Long id)
    {
        List<TDiagnosisRecord> tDiagnosisRecords = diagnosisRecordMapper.getDiagnosisRecordById(id);
        return tDiagnosisRecords.size() > 0 ? tDiagnosisRecords.get(0) : null;
    }

    @Override
    @Transactional
    public Boolean addDiagnosisRecord(Long doctorId, DiagnosisRecordDTO diagnosisRecord)
    {
        List<TUser> patient = diagnosisRecordMapper.getUserById(diagnosisRecord.getPatientId());
        List<TUser> doctor = diagnosisRecordMapper.getUserById(doctorId);
        if (patient.size() == 0 || doctor.size() == 0)
        {
            return false;
        }
        List<TPatientInfo> patientInfo = diagnosisRecordMapper.getPatientInfoById(patient.get(0).getInfoId());
        List<TDoctorInfo> doctorInfo = diagnosisRecordMapper.getDoctorInfoById(doctor.get(0).getInfoId());
        if (patientInfo.size() == 0 || doctorInfo.size() == 0)
        {
            return false;
        }
        TDiagnosisRecord tDiagnosisRecord = new TDiagnosisRecord(
                null,
                diagnosisRecord.getPatientId(),
                patientInfo.get(0).getMedicalCard(),
                patient.get(0).getName(),
                patient.get(0).getSex(),
                patientInfo.get(0).getIdentityCard(),
                patientInfo.get(0).getAddress(),
                patient.get(0).getPhone(),
                diagnosisRecord.getDrugAllergy(),
                diagnosisRecord.getDrugAllergyInfo(),
                diagnosisRecord.getRegistrationTime(),
                null,
                doctorInfo.get(0).getDepartment(),
                doctorInfo.get(0).getDepartmentId(),
                doctor.get(0).getName(),
                doctor.get(0).getId(),
                diagnosisRecord.getReferral(),
                diagnosisRecord.getChronicDiseases(),
                diagnosisRecord.getPatientComplaints(),
                diagnosisRecord.getPrimaryDiagnosis(),
                diagnosisRecord.getMedicationCosts(),
                diagnosisRecord.getConsultationFee(),
                diagnosisRecord.getMedication(),
                diagnosisRecord.getTotalCosts(),
                diagnosisRecord.getDiagnosisResult()
        );
        System.out.println(JsonUtils.toJson(tDiagnosisRecord));
        if (diagnosisRecordMapper.addDiagnosisRecord(tDiagnosisRecord) == 0)
        {
            throw new ServiceException(IMPORT_RECORD_FAILED);
        }

        Long result = hospitalFeign.addDiagnosisRecord(tDiagnosisRecord).getData();

        if (result == null || result == 0L)
        {
            //此处可有人工补偿记录代码

            throw new ServiceException(IMPORT_RECORD_FAILED, "医院系统提交失败");
        }
        Integer code = paymentFeign.create(new OrderCreateDto(diagnosisRecord.getPatientId(), OrderType.DIAGNOSIS.value, tDiagnosisRecord, null, null)).getCode();

        if (!code.equals(1))
        {
            throw new ServiceException(IMPORT_RECORD_FAILED, "支付系统提交失败，错误代码：" + code);
        }

        return true;
    }

    @Override
    public Boolean addDiagnosisApplication(Long doctorId, DiagnosisApplicationDTO diagnosis)
    {
        TDiagnosis tDiagnosis = new TDiagnosis(
                null,
                diagnosis.getPatientId(),
                doctorId,
                diagnosis.getInspectionName(),
                0.0,
                null
        );

        Long result = hospitalFeign.addDiagnosis(tDiagnosis).getData();
        if (result == null || result == 0L)
        {
            throw new ServiceException(IMPORT_RECORD_FAILED, "医院系统提交失败");
        }

//        if (diagnosisRecordMapper.addDiagnosisApplication(tDiagnosis) == 0)
//        {
//            throw new ServiceException(IMPORT_RECORD_FAILED);
//        }

        return true;
    }

    @Override
    public Integer getDiagnosisApplicationCountByPatientId(Long patientId, String doctorName, String startTime, String endTime)
    {
        return diagnosisRecordMapper.getDiagnosisApplicationCountByPatientId(patientId, doctorName, startTime, endTime);
    }

    @Override
    public List<DiagnosisVo> getDiagnosisApplicationByPatientId(Long patientId, String doctorName, String startTime, String endTime, Integer pageNum, Integer pageSize)
    {
        return diagnosisRecordMapper.getDiagnosisApplicationByPatientId(patientId, doctorName, startTime, endTime, (pageNum - 1) * pageSize, pageSize);
    }


    @Override
    public List<InspectionReportVo> getDiagnosisResultByPatientId(Long patientId)
    {
        return hospitalFeign.query(new InspectionReportDto(patientId)).getData();
    }

    @Override
    public Integer getDiagnosisRecordCountByName(Long patientId,Long doctorId,String patientName, String doctorName, String startTime, String endTime)
    {
        return diagnosisRecordMapper.getDiagnosisRecordCountByName(patientId,doctorId,patientName, doctorName, startTime, endTime);
    }

    @Override
    public List<DiagnosisRecordTitleVo> getDiagnosisRecordListByName(Long patientId,Long doctorId,String patientName, String doctorName, String startTime, String endTime, Integer pageNum, Integer pageSize)
    {
        return diagnosisRecordMapper.getDiagnosisRecordListByName(patientId,doctorId,patientName, doctorName, startTime, endTime, (pageNum - 1) * pageSize, pageSize);
    }


}